﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Crypto.Utilities;
using UserLicense.Applicationes.Command;
using UserModels;
using UserModels.DTO;
using UserRepositorys.Intface;

namespace UserLicense.Controllers
{
    [Authorize]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        private readonly IMediator mediator;
        public IUserRepository _IUserRepository;
        public IAccountRepository _IAccountRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public AccountController(IMediator mediator, IUserRepository userRepository, IAccountRepository iAccountRepository)
        {
            this.mediator = mediator;
            this._IUserRepository = userRepository;
            _IAccountRepository = iAccountRepository;
        }
        /// <summary>
        /// 添加账户信息
        /// </summary>
        /// <param name="accountAdd"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public async Task<int> AddAccount(AccountAddCommand accountAdd)
        {
            return await mediator.Send(accountAdd);
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userLogin"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public async Task<string> AccountLogin(AccountLoginCommand accountLogin)
        {
            return await mediator.Send(accountLogin);
        }

        /// <summary>
        /// 显示账户信息
        /// </summary>
        /// <param name="accountShow"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<List<AccountRoleDTO>> ShowAccount(AccountShowCommand accountShow)
        {
            return mediator.Send(accountShow);
        }

        /// <summary>
        /// 门店设置管理员
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Upadministrator(UpadministratorCommand command)
        {
            var rs = await mediator.Send(command);
            return Ok(rs);
        }
        /// <summary>
        /// 显示对应门店的管理员
        /// </summary>
        /// <param name="Sid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Fanministrator(int Sid)
        {
            var res = await _IUserRepository.GetAllAsync(x => x.StoreId == Sid);
            return Ok(res);
        }

        /// <summary>
        /// 删除指定账户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> DeleteAccount(int id)
        {
            return await _IAccountRepository.DeleteAccount(id);
        }

        /// <summary>
        /// 账户数据反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AccountInfoModel> GetAccountInfo(int id)
        {
            return await _IAccountRepository.GetAccountInfo(id);
        }

        /// <summary>
        /// 账户数据修改
        /// </summary>
        /// <param name="accountInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateAccount(AccountInfoModel accountInfo)
        {
            return await _IAccountRepository.UpdateAccount(accountInfo);
        }
    }
}
